﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Model.Manager;

namespace YiYiTong.Rocket.ManageWeb
{
    public class CurrentUser
    {
        public LoginReqModel user = null;
        public bool isLogin = false;

        public CurrentUser()
        {
            string ReturnUrl = HttpContext.Current.Request.RawUrl;
            //string str = "<script>top.location.href='/user/login?ReturnUrl=" + ReturnUrl + "'</script>";
            string str = "<script>top.location.href='/user/login'</script>";
            try
            {
                //get sessionid from cookies
                string lckey = ConfigHelper.AppSetting<string>("LoginCookieKey", "");
                string sid = Utils.GetCookie(lckey);

                if (!string.IsNullOrEmpty(sid) && sid.Length == 32)
                {
                    //check 
                    user = DataCacheHelper.Get<LoginReqModel>(sid);
                    if (user != null && !string.IsNullOrEmpty(user.loginName))
                    {
                        isLogin = true;
                    }
                    else
                    {
                        //跳转登录页面
                        HttpContext.Current.Response.Write(str);
                    }
                }
                else
                {
                    HttpContext.Current.Response.Write(str);
                }
            }
            catch
            {
                HttpContext.Current.Response.Write(str);
            }
        }
    }
}